package cn.sxt.yxl.common.util;

import java.util.Iterator;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class YxlSpiderUtil {

	public JSONObject spider(String columns,int page){
		String url = "http://www.xinli001.com/ceshi/"+columns;
		
		JSONObject jsonObject = new JSONObject();
		
		jsonObject.put("code", "0");
		JSONArray jsonArray = new JSONArray();
		
		try {
			//请求服务器
			Connection connection = Jsoup.connect(url+"?page="+page);
			//获取document对象
			Document document = connection.get();
			//获取页数
			Element pageEle = document.getElementById("pages");
			int totalPage = pageEle.getElementsByTag("a").size()-2;
			String toPage = pageEle.getElementsByTag("a").get(totalPage).text();
			jsonObject.put("totalPage", toPage);
			//获取父节点的对象  
			Element listShow = document.getElementsByClass("list_show").get(0);
//			ConstatFinalUtil.LOGGER.info("listShow:{}",listShow.size());
			//取子节点对象
			Elements fixed = listShow.getElementsByAttributeValue("class", "fixed");
//			ConstatFinalUtil.LOGGER.info("listShow:{}",name.size());
			for (Iterator iterator = fixed.iterator(); iterator.hasNext();) {
				Element element = (Element) iterator.next();
				Element flEle = element.getElementsByClass("linfo fl").get(0);
				//获取标题名
				String name = flEle.getElementsByTag("a").text();
				String img = element.getElementsByTag("img").get(0).attr("src");
				String hrefId = element.getElementsByTag("a").get(0).attr("href");
				String id = hrefId.substring(hrefId.lastIndexOf("/")+1, hrefId.length());
				//获取href路径
				String href = flEle.getElementsByTag("a").attr("href");
//				ConstatFinalUtil.LOGGER.info("----name:{},href:{},img:{}",name,href,img);
				JSONObject itemsJson = new JSONObject();
				itemsJson.put("title", name);
				itemsJson.put("href", href);
				itemsJson.put("imgPath", img);
				itemsJson.put("id", id);
				jsonArray.add(itemsJson);
			}
		} catch (Exception e) {
			ConstatFinalUtil.LOGGER.error("报错了~");
		}
		jsonObject.put("data", jsonArray);
		return jsonObject;
	}
}
